import pymysql
import json

with open('piaofang.json', 'r', encoding='utf-8') as file:
    data = json.load(file)
# 打开数据库连接
db = pymysql.connect(host='localhost', user='root', password='123456', database='film')

cursor = db.cursor()  # 使用 cursor() 方法创建一个游标对象 cursor
for i in data["list"]:
    movieName = i["movieInfo"]["movieName"]
    sumBoxDesc = i["sumBoxDesc"]
    time = i["movieInfo"]['releaseInfo']
    count = i['showCount']
    sql = "INSERT INTO piaofang (`filmname`,`money`,`time`,`count`) values('%s','%s','%s','%s')" % (
    movieName, sumBoxDesc, time, count)
    print(sql)
    cursor.execute(sql)  # 使用 execute()  方法执行 SQL 查询
db.commit()
# 关闭数据库连接
db.close()
